package com.kuang;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @Auther: LIJingQi
 * @Description: excel写测试
 * @Date: 2022/5/28 12:20
 */
public class excelWriteTest {

    String PATH="C:\\Users\\73161\\Desktop\\mySelf\\springboot-redis-first-try\\poi";
    @Test
    public void testWrite03() throws Exception {
//        创建一个工作铺
        Workbook workbook =new HSSFWorkbook();
//        创建一个工作表
        Sheet sheet = workbook.createSheet("工作表默认名字");
//        创建一个行
        Row row1 = sheet.createRow(0);
//        创建一个单元格
        Cell cell = row1.createCell(0);
        cell.setCellValue("第一个单元格");
        row1.createCell(1).setCellValue("第一行第二个单元格");

//        第二行
        sheet.createRow(1).createCell(2).setCellValue("第二行第三个单元格");
//        日期格式化
        String string = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        workbook.createSheet("第二个工作表").createRow(0).createCell(0).setCellValue(string);

//        生成一个表 IO
        FileOutputStream outputStream = new FileOutputStream(PATH+"创建03excel尝试.xls");
        workbook.write(outputStream);
        //关闭流
        outputStream.close();
    }



    @Test
    public void testWrite07() throws Exception {
//        创建一个工作铺
        Workbook workbook = new XSSFWorkbook();
//        创建一个工作表
        Sheet sheet = workbook.createSheet("工作表默认名字");
//        创建一个行
        Row row1 = sheet.createRow(0);
//        创建一个单元格
        Cell cell = row1.createCell(0);
        cell.setCellValue("第一个单元格");
        row1.createCell(1).setCellValue("第一行第二个单元格");

//        第二行
        sheet.createRow(1).createCell(2).setCellValue("第二行第三个单元格");
//        日期格式化
        String string = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        workbook.createSheet("第二个工作表").createRow(0).createCell(0).setCellValue(string);

//        生成一个表 IO
        FileOutputStream outputStream = new FileOutputStream(PATH+"创建07excel尝试.xlsx");
        workbook.write(outputStream);
        //关闭流
        outputStream.close();
    }

    @Test
    public void testWrite03Time() throws Exception {
//        系统开始时间
        long begin = System.currentTimeMillis();
//        创建一个工作铺
        Workbook workbook =new HSSFWorkbook();
//        创建一个工作表
        Sheet sheet = workbook.createSheet("工作表默认名字");
//        创建一个行
        for (int i = 0; i < 65535; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(j);

            }
        }
//        生成一个表 IO
        FileOutputStream outputStream = new FileOutputStream(PATH+"testWrite03Time.xls");
        workbook.write(outputStream);
        //关闭流
        outputStream.close();
        long end = System.currentTimeMillis();
          Double a= (double)(end - begin) / 1000;

        System.out.println(a);

    }

    @Test
    public void testWrite07Time() throws Exception {
//        系统开始时间
        long begin = System.currentTimeMillis();
//        创建一个工作铺
        Workbook workbook =new XSSFWorkbook();
//        创建一个工作表
        Sheet sheet = workbook.createSheet();
//        创建一个行
        for (int i = 0; i < 65537; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(j);

            }
        }
        System.out.println("生成工作铺完毕，开始写入excel");
//        生成一个表 IO
        FileOutputStream outputStream = new FileOutputStream(PATH+"testWrite07Time.xlsx");
        workbook.write(outputStream);
        //关闭流
        outputStream.close();
        long end = System.currentTimeMillis();
        Double a= (double)(end - begin) / 1000;

        System.out.println(a);

    }

    @Test
    public void testWrite07TimeS() throws Exception {
//        系统开始时间
        long begin = System.currentTimeMillis();
//        创建一个工作铺
        Workbook workbook =new SXSSFWorkbook ();
//        创建一个工作表
        Sheet sheet = workbook.createSheet();
//        创建一个行
        for (int i = 0; i < 65537; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < 10; j++) {
                Cell cell = row.createCell(j);
                cell.setCellValue(j);

            }
        }
        System.out.println("生成工作铺完毕，开始写入excel");
//        生成一个表 IO
        FileOutputStream outputStream = new FileOutputStream(PATH+"testWrite07TimeS.xlsx");
        workbook.write(outputStream);
        //关闭流
        outputStream.close();
//        清除零时文件
        ((SXSSFWorkbook)workbook).dispose();
        long end = System.currentTimeMillis();
        Double a= (double)(end - begin) / 1000;

        System.out.println(a);

    }

}
